<?php 
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class ArticleController extends AdminBaseController{
	public function index(){
		$page = I('get.page/d',1);
		$title = I('get.title/s','');
		$wheresql = empty($title) ? '' : 'title like "%'. $title .'%" or content like "%'.$title.'%" or author like "%'.$title.'%"';
		$num = 5;
		$totalnum = M('Article') -> where($wheresql) -> count();
		$pagenum  = ceil($totalnum/$num);
		$frow = empty($page - 1) ? 0 : (($page - 1) * $num);
		$article = D('article');
		$list = $article -> artlist(0,'a.id,a.cat_id,a.title,a.author,a.img,a.seo_keywords,a.seo_description,a.content,a.addtime,a.is_html,a.addman,a.hits,a.sort,c.coltitle','a','left join dx_columns c on a.cat_id = c.id',$wheresql,$frow,$num);
		$this -> assign('list',$list['data']);
		$this -> assign('title',$title);
		$this -> assign('page', $pagenum);
		$this -> display();
	}

	public function add(){
		$col = M('Columns');
		$list = $col -> field('id,coltitle') -> where(['type' => 1]) -> select(); 
		$this -> assign('opt',$list);
		$this -> display();
	}
	
	public function save(){
		if(IS_POST || IS_AJAX){
			$result = D('article') -> addArt(I('post.'));
			$this -> ajaxReturn($result);
		}	
	}

	public function edit(){
		if(IS_GET){
			$col = M('Columns');
			$list = $col -> field('id,coltitle') -> where(['type' => 1]) -> select();
			$result = D('article') -> artlist(I('get.id/d',0),'*');//三维数组
			$this -> assign('opt',$list);
			$this -> assign('result',$result['data'][0]);
			$this -> display('Article_add');
		}
	}
	
	public function del(){
		if(IS_POST){
			$result = D('article') -> Delart(I('post.id/d',0));
			$this -> ajaxReturn($result);
		}
	}

	public function upload(){
		if(IS_GET){
			$id = I('get.id/d',0);
			$result = M('Article') -> field('img') -> where(['id' => $id]) -> find();
			$img = implode(',', $result);
			$imgarr = explode(',', rtrim($img,','));   
			$this -> assign('list',$imgarr);
			$this -> display();
		}
	}
	




}



